package com.hitqz.robot.biz.flow.component.agv;

import com.alibaba.fastjson.JSONObject;
import com.hitqz.robot.api.business.model.dto.FlowContextDto;
import com.hitqz.robot.api.business.model.dto.FlowPatrolPreAndEndTicketsDto;
import com.hitqz.robot.api.common.entity.Robot;
import com.hitqz.robot.biz.flow.util.LiteFlowUtil;
import com.hitqz.robot.biz.service.RobotService;
import com.yomahub.liteflow.annotation.LiteflowComponent;
import com.yomahub.liteflow.core.NodeComponent;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.client.RestTemplate;

import java.util.Optional;

/**
 * @author xupkun
 * @date 2025/4/18
 */
@Slf4j
@LiteflowComponent(value = "AgvInRequest", name = "[轮对/轮轴]进入设备前停止机械臂")
@AllArgsConstructor
public class AgvInRequest extends NodeComponent {

    private final RestTemplate restTemplate;

    private final RobotService robotService;



    @Override
    public void process() throws Exception {
        try {
            String uri = "http://192.10.13.20:9999/lzms/agvInRequest?inSbbh=123&agvSbbh=345&pjid=";
            LiteFlowUtil.checkInterruptFlag();
            FlowContextDto dto =  this.getRequestData();
            Robot robot = robotService.getDefaultRobot();
            FlowPatrolPreAndEndTicketsDto ticketsDto = JSONObject.parseObject(dto.getTicketsTask()
                    .getParseOrderParams(), FlowPatrolPreAndEndTicketsDto.class);
            String targetPosName = Optional.ofNullable(ticketsDto.getModalData().get("sourcePosName"))
                    .orElse(ticketsDto.getModalData().get("sourcePosCode")).toString();
            uri = uri.replace("123", targetPosName).replace("345", robot.getRobotName());
            String result = restTemplate.postForObject(uri, "", String.class);
            log.info("agv in request : {} , {}", uri, result);
        } catch (Exception e) {

        }

    }
}
